<?php
/***************************************************************************
 *
 *   copyright            : (C) 2005 Winds of Storm
 *
 *   $Id: ban.php,v 1.3 2006/01/11 07:21:42 nathan Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

$ban_commands	= array('TYPE'		=>	'command',
			'NAME'		=>	'Banning Commands',
			'VERSION'	=>	'0.0.1',
			'CREDIT'	=>	'StormeRider',
			'FUNC'		=>	'pluginBanCommands',
			'HELP'		=>	'pluginBanCommandsHelp');
$plugins[]	= $ban_commands;

function pluginBanCommandsHelp($help) {
	global	$ban_commands;

	$help['userban1']	= array("/userban [user] [message]",
					"Ban [user] from chat with [message]." .
					pluginCredit($ban_commands));
	$help['userban2']	= array("/userban '[user]' [message]",
					"Ban [user] from chat with [message]." .
					pluginCredit($ban_commands));
	$help['userban3']	= array("/userban \"[user]\" [message]",
					"Ban [user] from chat with [message]." .
					pluginCredit($ban_commands));

	return $help;
}

function pluginBanCommands($userinfo, $event) {
	global	$db;
	global	$dbprefix;
	global	$p_row;
	global	$username;

	$username	= $userinfo['username'];
	$userid		= $userinfo['userid'];
	$isMod		= $userinfo['isMod'];

	$msg	= $event[0]['msg'];
	$room	= $event[0]['room'];

	if (eregi("^/userban", $msg)) {
		$event[0]['parsed']	= 1;
		$msg	= stripslashes($msg);

		if (!$isMod) {
                        $event[0]['userto']     = $userid;
                        $event[0]['room']       = 0;
			$event[0]['msg']	= addslashes('<span class=sysmsg><b>*** ERROR: you are not authorized to ban members.</b></span>');
		} else if (eregi("^/userban '([^']*)' (.*)", $msg, $matches)) {
                        $user	= $matches[1];
                        $bmsg	= $matches[2];
                } else if (eregi("^/userban \"([^']*)\" (.*)", $msg, $matches)) {
                        $user	= $matches[1];
                        $bmsg	= $matches[2];
                } else if (eregi("^/userban ([^ ]*) (.*)", $msg, $matches)) {
                        $user	= $matches[1];
                        $bmsg	= $matches[2];
                } else {
                        $event[0]['userto']     = $userid;
                        $event[0]['room']       = 0;
                        $event[0]['msg']        = "<span class=sysmsg><b>*** ERROR: could not parse the '/userban' command.</b></span>";
                }

		if ($event[0]['room'] != 0) {
			$uid    = getOnlineUserIdByName($user);
			$event[0]['room']	= 0;
			$event[0]['userto']	= $userid;

			if ($uid == -1) {
				$event[0]['msg']        = addslashes("<span class=sysmsg><b>*** ERROR: '$user' is not online to ban: $bmsg.</b></span>");
			} else {
				$bsmg	= addslashes($bmsg);
				$sql	= "INSERT INTO {$dbprefix}bans (ban_type, ban_value, ban_date, ban_message, ban_banner)" .
						" VALUES ('userid', '$uid', NOW(), '$bmsg', $userid)";
				$res	= $db->sql_query($sql);
				$bmsg	= stripslashes($bmsg);

				$event[0]['msg']	= addslashes("<span class=sysmsg>*** Banned $user with message $bmsg</span>");
			}
			//$event[0]['msg']        = addslashes("*** uid = $uid<br />*** user = $user<br />*** bsmg = $bmsg");
		}
	} else if (eregi("^/ipban", $msg)) {
		$event[0]['parsed']	= 1;
		$event[0]['room']	= 0;
		$event[0]['userto']	= $userid;
		if (!$isMod) {
			$event[0]['msg']	= addslashes('<span class=sysmsg><b>*** ERROR: you are not authorized to ban members.</b></span>');
		} else {
			$ipban	= explode(" ", $msg, 3);
			$ip	= $ipban[1];
			$imsg	= addslashes($ipban[2]);

			if ($imsg != '') {
				$sql	= "INSERT INTO {$dbprefix}bans (ban_type, ban_value, ban_date, ban_message, ban_banner)" .
						" VALUES ('ip', '$ip', NOW(), '$imsg', $userid)";
				$res	= $db->sql_query($sql);
				$imsg	= stripslashes($imsg);

				$event[0]['msg']	= addslashes("<span class=sysmsg>*** Banned $ip with message $imsg</span>");
			} else {
				$event[0]['msg']        = addslashes("<span class=sysmsg>*** ERROR: Could not parse $msg</span>");
			}
		}
	}
}
?>
